package com.kuaiditu.assistant.ui.main;

import android.content.Intent;
import android.os.Handler;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.kuaiditu.assistant.R;
import com.kuaiditu.assistant.api.assistant_api.AssistantApi;
import com.kuaiditu.assistant.base.BaseActivity;
import com.kuaiditu.assistant.common.Constants;
import com.kuaiditu.assistant.event.LocationEvent;
import com.kuaiditu.assistant.event.NeedReloadEvent;
import com.kuaiditu.assistant.module.update.UpdateBeanCallBack;
import com.kuaiditu.assistant.module.update.UpdateHttpManager;
import com.vector.update_app.UpdateAppManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


@Route(path = MainActivity.PATH)
public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    public static final String PATH = "/index/main";

    private FragmentManager mFragmentManager;
    private int container_layout_id;

    private RadioGroup mRgTaber;

    boolean mIsExit;

    AssistantApi mAssistantApi;

    private TextView mTvRefresh;

    private boolean needReload;


    @Override
    public int initContentView() {
        return R.layout.activity_main;
    }

    @Override
    public void initInjector() {
        mAssistantApi = getApplicationComponent().getApiManager().getAssistantApi();

    }

    @Override
    public void initUiAndListener() {

        EventBus.getDefault().register(this);
        container_layout_id = R.id.container_layout;
        mFragmentManager = getSupportFragmentManager();
        mRgTaber = (RadioGroup) findViewById(R.id.rg_main_taber);
        mTvRefresh = (TextView) findViewById(R.id.tv_refresh);
        mTvRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TaskSquareFragment resFragment = (TaskSquareFragment) mFragmentManager.findFragmentByTag(TaskSquareFragment.TAG);
                if (resFragment != null) {
                    mRgTaber.check(R.id.ra_task_square);
                    resFragment.onRefresh();
                }
            }
        });
        mRgTaber.setOnCheckedChangeListener(this);
        showFragmentByTag(R.id.ra_task_square);
        checkVer();


    }


    private synchronized void showFragmentByTag(int id) {
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        if (mFragmentManager.getFragments() != null) {
            for (Fragment fragment : mFragmentManager.getFragments()) {
                if (fragment != null) {
                    fragmentTransaction.hide(fragment);
                }

            }
        }
        Fragment resFragment = null;
        switch (id) {
            case R.id.ra_task_square:
                resFragment = mFragmentManager.findFragmentByTag(TaskSquareFragment.TAG);
                if (resFragment == null) {

                    resFragment = new TaskSquareFragment();

                    fragmentTransaction.add(container_layout_id, resFragment, TaskSquareFragment.TAG);

                }
                break;
            case R.id.ra_task_person:
                resFragment = mFragmentManager.findFragmentByTag(MyTaskFragment.TAG);
                if (resFragment == null) {
                    resFragment = new MyTaskFragment();
                    fragmentTransaction.add(container_layout_id, resFragment, MyTaskFragment.TAG);
                }
                break;
        }
        fragmentTransaction.show(resFragment);
        fragmentTransaction.commit();
    }


    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        showFragmentByTag(checkedId);
    }


    @Override
    public void onBackPressed() {
        if (mIsExit) {
            this.finish();
        } else {
            Toast.makeText(this, "再按一次退出应用", Toast.LENGTH_SHORT).show();
            mIsExit = true;
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    mIsExit = false;
                }
            }, 2000);
        }
    }


    private void checkVer() {
        UpdateAppManager.Builder builder = new UpdateAppManager.Builder();
        builder.setActivity(this)
                .setHttpManager(new UpdateHttpManager())
                .setUpdateUrl(Constants.BASE_URL + "/partTimeCourierApi/kuaiditu/api")
                .setPost(true)
                .setAppKey("11")
                .hideDialogOnDownloading(false)
                .build()
                .checkNewApp(new UpdateBeanCallBack());

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (needReload) {
            reload();
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void needReload(NeedReloadEvent event) {
        needReload = true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void needNoticeChange(LocationEvent event) {
        TaskSquareFragment resFragment = (TaskSquareFragment) mFragmentManager.findFragmentByTag(TaskSquareFragment.TAG);
        if (resFragment != null) {
            resFragment.noticeChange();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
